package edu.drexel.jrexel2d.input;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.lwjgl.input.Keyboard;

public class InputMap {
	
	GameInput gameInput;

	public InputMap(){
		gameInput = new GameInput();
		
	}
	
	private Map<Integer, GameAction> keyboardMap; 
	
	public void addToKeyboardMap(int keyboardKey, GameAction gameAction){
		if(keyboardMap ==null){
			keyboardMap = new HashMap<Integer, GameAction>();
		}
		keyboardMap.put(keyboardKey, gameAction);
		
	}
	
	public GameInput getUserInput()
	{
		
		/*//this implementation isnt able to support simultaneous key presses
		Keyboard.enableRepeatEvents(true);	
		while(Keyboard.next()){
				if (Keyboard.getEventKeyState()==true){
					int keyPressed = Keyboard.getEventKey();
					System.out.println("Keypressed: " + keyPressed);
					GameAction action = keyboardMap.get((Integer)keyPressed);
					if(action !=null)gameInput.addGameAction(action);
			}
		}*/

		Set<Entry<Integer, GameAction>> keySet = keyboardMap.entrySet();
		for (Entry<Integer, GameAction> entry : keySet) {
			int key = entry.getKey();
			if (Keyboard.isKeyDown(key)) {
				//System.out.println("Key pressed: " + key);
				GameAction action = entry.getValue();
				if (action != null)
					gameInput.addGameAction(action);
			}	
		}
		
		return gameInput;
		
	}
	public GameInput getGameInputObject(){
		return gameInput;
	}
}
